<template>
  <div class="app-container">
    <div class="title">公告管理</div>
    <el-card shadow="never" class="table-container">
      <template #header>
        <el-form
          ref="queryFormRef"
          :model="queryParams"
          :inline="true"
          style="margin-top: 10px"
        >
          <el-form-item prop="keywords" label="名称">
            <el-input
              v-model="queryParams.keywords"
              placeholder="输入名称"
              clearable
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleQuery"
              ><i-ep-search />搜索</el-button
            >

            <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
          </el-form-item>

          <el-form-item>
            <el-button type="success" @click="openDialog()"
              ><i-ep-plus />新增</el-button
            >
          </el-form-item>
        </el-form>
      </template>
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        :stripe="true"
        size="large"
      >
        <el-table-column align="center" label="序号" type="index" width="100" />
        <el-table-column align="center" label="标题" prop="title" />
        <el-table-column align="center" label="公告内容" prop="content" />
        <el-table-column align="center" label="发布时间" prop="createTime" />
        <el-table-column align="center" label="修改时间" prop="updateTime" />
        <el-table-column align="center" label="发布人" prop="createBy" />
        <el-table-column align="center" label="状态" prop="status">
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.status == 0">正常</el-tag>
            <el-tag type="primary" v-if="scope.row.status == 1">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="180">
          <template #default="scope">
            <el-button
              type="primary"
              text
              size="small"
              @click.stop="openDialog(scope.row.noticeId)"
              ><i-ep-edit />编辑
            </el-button>
            <el-popconfirm
              title="你确定需要删除?"
              @confirm="handleDelete(scope.row.noticeId)"
            >
              <template #reference>
                <el-button type="danger" text size="small">
                  <i-ep-delete />删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 公告表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="30%"
      @close="closeDialog"
    >
      <el-form
        ref="dataFormRef"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="formData.title" placeholder="请输入标题" />
        </el-form-item>

        <el-form-item label="公告封面" prop="noticeImg">
          <MultiUpload
            :model-value="FileValue"
            :limit="FileLimit"
            @update:model-value="uploadFile"
          />
        </el-form-item>

        <el-form-item label="公告内容" prop="content">
          <el-input
            v-model="formData.content"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>

        <el-form-item label="是否展示" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="0">展示</el-radio>
            <el-radio :label="1">不展示</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {
  getPageNoticeAPI,
  getFormNoticeAPI,
  addNoticeAPI,
  updateNoticeAPI,
  deleteNoticeAPI,
} from "@/api/business/notice/index";

import { IPage, INotice } from "@/api/business/notice/types";

import { ref, reactive } from "vue";
import MultiUpload from "@/components/Upload/MultiUpload.vue";

const total = ref(0);
const loading = ref(false);
const tableData = ref<INotice[]>([]);
const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);
// 表单数据
const formData = reactive<INotice>({});
const FileValue = ref<Array<String>>();
const FileLimit = ref<Number>(1);
// 表单规则
const rules = reactive({
  title: [{ required: true, message: "请输入", trigger: "blur" }],
});

const dialog = reactive<DialogOption>({
  visible: false,
});
const queryParams = reactive<IPage>({
  pageNum: 1,
  pageSize: 10,
});

onMounted(() => {
  handleQuery();
});

// 获取公告列表
function handleQuery() {
  loading.value = true;
  getPageNoticeAPI(queryParams)
    .then(({ data }) => {
      tableData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */

function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 关闭表单弹窗 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/** 打开公告表单弹窗 */
function openDialog(noticeId?: number) {
  FileValue.value = [];
  console.log("公告", noticeId);
  if (noticeId) {
    dialog.title = "修改公告";
    getFormNoticeAPI(noticeId).then(({ data }) => {
      Object.assign(formData, data);
      FileValue.value = [data.noticeImg];
    });
  } else {
    dialog.title = "新增公告";
  }
  dialog.visible = true;
}

/** 公告保存提交 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const noticeId = formData.noticeId;
      if (noticeId) {
        updateNoticeAPI(noticeId, formData)
          .then(() => {
            ElMessage.success("修改成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        addNoticeAPI(formData)
          .then(() => {
            ElMessage.success("新增成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 重置表单 */
function resetForm() {
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.noticeId = undefined;
}
/** 删公告 */
function handleDelete(noticeId: number) {
  loading.value = true;
  deleteNoticeAPI(noticeId)
    .then(() => {
      ElMessage.success("删除成功");
      resetQuery();
    })
    .finally(() => (loading.value = false));
  resetForm();
}
/**
 * 文件上传返回结果
 * @param result
 */
const uploadFile = (result: Array<String>) => {
  formData.noticeImg = result[0];
};
</script>

<style lang="scss" scoped></style>
